﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StyleMVVM.View.Impl;
#if NETFX_CORE
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

#else
using System.Windows.Navigation;
using System.Windows;
using System.Windows.Controls;

#endif

namespace StyleMVVM.View
{
	public sealed class FrameHelper
	{
		public static string GetStartPage(DependencyObject obj)
		{
			return (string)obj.GetValue(InternalStartPageProperty);
		}

		public static void SetStartPage(DependencyObject obj, string value)
		{
			obj.SetValue(InternalStartPageProperty, value);
		}

		public static DependencyProperty StartPageProperty
		{
			get { return InternalStartPageProperty; }
		}

		// Using a DependencyProperty as the backing store for StartPage.  This enables animation, styling, binding, etc...
		internal static readonly DependencyProperty InternalStartPageProperty =
			DependencyProperty.RegisterAttached("StartPage",
			                                    typeof(string),
			                                    typeof(FrameHelper),
			                                    new PropertyMetadata(null, StartPageChanged));

		private static void StartPageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			Frame frame = d as Frame;

			if (frame != null)
			{
				string navigationTarget = e.NewValue as string;

				if (!string.IsNullOrEmpty(navigationTarget))
				{
					INavigationService navigationService =
						Bootstrapper.Instance.Container.LocateByType(typeof(INavigationService), frame) as INavigationService;

#if NETFX_CORE
					navigationService.Navigate(navigationTarget);
#elif WINDOWS_PHONE
					navigationService.Navigate(new Uri(string.Format("/Views/{0}.xaml", navigationTarget)));
#else
					navigationService.Navigate(navigationTarget);
#endif
				}
			}
			else
			{
				throw new Exception("This property is only applicable to Frame controls");
			}
		}

#if DOT_NET

		public static bool GetIsChildViewModelNavigationEnabled(DependencyObject obj)
		{
			return (bool)obj.GetValue(IsChildViewModelNavigationEnabledProperty);
		}

		public static void SetIsChildViewModelNavigationEnabled(DependencyObject obj, bool value)
		{
			obj.SetValue(IsChildViewModelNavigationEnabledProperty, value);
		}

		// Using a DependencyProperty as the backing store for IsChildViewModelNavigationEnabled.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty IsChildViewModelNavigationEnabledProperty =
			DependencyProperty.RegisterAttached("IsChildViewModelNavigationEnabled",
			                                    typeof(bool),
			                                    typeof(FrameHelper),
			                                    new PropertyMetadata(false, ChildEnabledChanged));

		private static void ChildEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			Frame frame = d as Frame;

			if (frame != null)
			{
				if ((bool)e.NewValue)
				{
					frame.Navigated += FrameOnNavigated;
					frame.Navigating += FrameOnNavigating;
				}
			}
		}

		private static void FrameOnNavigating(object sender, NavigatingCancelEventArgs navigatingCancelEventArgs)
		{
		}

		private static void FrameOnNavigated(object sender, NavigationEventArgs navigationEventArgs)
		{
		}

#endif
	}
}